<template>
	<ele-modal
		:width="750"
		:visible="visible"
		:confirm-loading="loading"
		:title="actionStatus == 'edit' ? '修改' : '新建'"
		:body-style="{ paddingBottom: '8px' }"
		@update:visible="updateVisible"
		@ok="save"
	>
		<a-form :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }" :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }">
			<a-row :gutter="24">
				<a-col :md="20" :sm="24" :xs="24">
					<a-form-item label="修改原因" v-bind="validateInfos.alter_cause">
						<a-textarea
							placeholder="请输入修改原因"
							allow-clear
							v-model:value="form.alter_cause"
						/>
					</a-form-item>
					<a-form-item label="业务日期" v-bind="validateInfos.business_time">
						<a-date-picker
						style="width:100%;"
						  value-format="YYYY-MM-DD HH:mm:ss"
						  show-time
						  autocomplete="off"
						  placeholder="请选择业务日期"
						  v-model:value="form.business_time"
						/>
					</a-form-item>
					<a-form-item label="备注">
						<a-textarea
							placeholder="请输入备注"
							allow-clear
							v-model:value="form.note"
						/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
	</ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Form } from 'ant-design-vue';
  import { emailReg, phoneReg, identityReg } from 'ele-admin-pro';
  import { receipts_purchase_detail } from '@/api/jewelry/receipts_alter_goods';

  // 表格列配置
  const useForm = Form.useForm;

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    // 编辑edit/新建add
    actionStatus: String,
    // 编辑数据id
    editData: Object,
  });

  const emit = defineEmits(['done', 'update:visible']);

  // 提交状态
  const loading = ref(false);
  onMounted(() => {
    if (props.editData.code) {
      form.code = props.editData.code;
      receipts_purchase_detail({ code: props.editData.code }).then(
        (res) => {
          Object.keys(form).forEach((key) => {
            form[key] = res.receipts_detail ? res.receipts_detail[key] : '';
          });
        }
      );
    }
  });

  // 表单数据
  const form = reactive({
    code: '',
    alter_cause: '',
    business_time: '',
    note: ''
  });
  // 表单验证规则
  const rules = reactive({
    alter_cause: [
      {
        required: true,
        message: '请输入修改原因',
        trigger: 'change'
      }
    ],
    business_time: [
      {
        required: true,
        message: '请选择业务日期',
        trigger: 'change'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);
  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        emit('done', form);
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
